package service

import (
	"egin/app/models"
	"egin/app/request"
	"egin/app/responses"
	"egin/pkg/database"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

func PermissionRoleTree() []*responses.PermissionRoleTree {
	var permissionList []models.Permission
	database.Db().Order("sort asc").Find(&permissionList)
	return buildPermissionChild(0, permissionList)
}

//递归获取子权限
func buildPermissionChild(pid int, permissionList []models.Permission) []*responses.PermissionRoleTree {
	var treeList []*responses.PermissionRoleTree
	for _, v := range permissionList {
		if pid == v.PID {

			node := &responses.PermissionRoleTree{
				Id:     int(v.ID),
				Title:  v.Title,
				Spread: true,
				Pid:    v.PID,
			}
			child := buildPermissionChild(int(v.ID), permissionList)
			if len(child) != 0 {
				node.Children = child
			}
			// todo 后续此处加上用户的权限判断
			treeList = append(treeList, node)
		}
	}
	return treeList
}

//递归获取子权限
func buildPermissionListChild(pid int, permissionList []models.Permission) []*responses.PermissionList {
	var treeList []*responses.PermissionList
	for _, v := range permissionList {
		if pid == v.PID {

			node := &responses.PermissionList{
				Id:         int(v.ID),
				Title:      v.Title,
				Href:       v.Href,
				Icon:       v.Icon,
				FontFamily: v.FontFamily,
				Sort:       v.Sort,
				IsMenu:     v.IsMenu,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				Spread:     true,
				Pid:        v.PID,
			}
			child := buildPermissionListChild(int(v.ID), permissionList)
			if len(child) != 0 {
				node.Children = child
			}
			// todo 后续此处加上用户的权限判断
			treeList = append(treeList, node)
		}
	}
	return treeList
}

/**
*GetPermissionList 获取权限列表数据
*@return database.Paginate 数据（分页）
*@return error 错误
 */
func GetPermissionList(c *gin.Context) ([]*responses.PermissionList, error) {
	permissionobj := database.Db()

	if title := c.PostForm("title"); title != "" {
		permissionobj = permissionobj.Where("title = ?", title)
	}
	if href := c.PostForm("href"); href != "" {
		permissionobj = permissionobj.Where("href = ?", href)
	}

	var list []models.Permission

	err := permissionobj.Order("sort asc").Find(&list).Error
	if err != nil {
		zap.L().Error("GetPermissionList", zap.Error(err))
		return nil, err
	}

	return buildPermissionListChild(0, list), nil

}

func GetPermission(c *gin.Context) (*models.Permission, error) {
	id := c.Param("id")

	var list *models.Permission

	err := database.Db().Where("id = ?", id).Find(&list).Error
	if err != nil {
		zap.L().Error("GetPermissionList", zap.Error(err))
		return nil, err
	}

	return list, err
}

/**
*PermissionAdd 添加权限
*@param param  request.PermissionQs 绑定的请亲数据
*@return error
 */
func PermissionAdd(param request.PermissionQs, c *gin.Context) error {

	permission := models.Permission{
		Title:      param.Title,
		Href:       param.Href,
		Icon:       param.Icon,
		IsMenu:     param.IsMenu,
		PID:        *param.PID,
		Sort:       param.Sort,
		FontFamily: param.FontFamily,
	}

	result := database.Db().Create(&permission)
	return result.Error
}

/**
*PermissionEdit 修改权限
*@param param  request.PermissionQs 绑定的请亲数据
*@return error
 */
func PermissionEdit(param request.PermissionEditQs, c *gin.Context) error {

	var permission models.Permission
	database.Db().Where("id = ?", param.Id).First(&permission)

	permission.Title = param.Title
	permission.Href = param.Href
	permission.Icon = param.Icon
	permission.IsMenu = param.IsMenu
	permission.PID = *param.PID
	permission.Sort = param.Sort
	permission.FontFamily = param.FontFamily
	result := database.Db().Save(&permission)
	return result.Error
}

/**
*PermissionDelete 删除权限
*@return error
 */
func PermissionDelete(c *gin.Context) error {
	id := c.Param("id")
	err := database.Db().Where("id = ?", id).Delete(&models.Permission{}).Error
	return err
}
